<?php

class SeoModel extends Models
{
    private $transliterate=array ("А"=>"A","Б"=>"B","В"=>"V","Г"=>"G","Д"=>"D","Е"=>"E","Ж"=>"J","З"=>"Z","И"=>"I","Й"=>"Y","К"=>"K","Л"=>"L","М"=>"M","Н"=>"N",
        "О"=>"O","П"=>"P","Р"=>"R","С"=>"S","Т"=>"T","У"=>"U","Ф"=>"F","Х"=>"H","Ц"=>"TS","Ч"=>"CH","Ш"=>"SH","Щ"=>"SCH","Ъ"=>"","Ы"=>"YI",
        "Ь"=>"","Э"=>"E","Ю"=>"YU","Я"=>"YA","а"=>"a","б"=>"b","в"=>"v","г"=>"g","д"=>"d","е"=>"e","ж"=>"j","з"=>"z","и"=>"i","й"=>"y","к"=>"k",
        "л"=>"l","м"=>"m","н"=>"n","о"=>"o","п"=>"p","р"=>"r","с"=>"s","т"=>"t","у"=>"u","ф"=>"f","х"=>"h","ц"=>"ts","ч"=>"ch","ш"=>"sh","щ"=>"sch",
        "ъ"=>"y","ы"=>"yi","ь"=>"'","э"=>"e", "ё"=>"e", "Ё"=>"e", "ю"=>"yu","я"=>"ya","."=>"-"," "=>"-","?"=>"-","/"=>"-","\\"=>"-","*"=>"-",":"=>"-",
        "*"=>"-",">"=>"-","|"=>"-","'"=>"");

    public function createSeoWord($name){
        $name=$this->transliterate($name);
        $randWord='';
        $subWord=rand(0, 9);

        for($i=0; $i<4; $i++){
            $subWord.=rand(0, 9);
        }
        if(!$name){
            $symbols=array_values($this->transliterate);
            for($i=0; $i<4; $i++){
                $symbol=$symbols[rand(0, 9)];
                if($symbol=='-' || $symbol=='_' || $symbol=='.') $symbol='w';
                $randWord.=$symbol.$subWord;
            }
        }
        return strtolower($subWord.'-'.$name.$randWord);
    }

    public function generateSeoUrl($data){

        if(isset($data['category_description'])){
            $description='category_description';
            $name='name';
        }elseif(isset($data['information_description'])){
            $description='information_description';
            $name='title';
        }elseif(isset($data['product_description'])){
            $description='product_description';
            $name='name';
        }else{
            return false;
        }

        $my_title='';

        foreach($data[$description] as $value){
            if($value && isset($value[$name])){
                $my_title=$value[$name];
                break;
            }
        }

        return $this->transliterate($my_title);
    }

    public function transliterate($name)
    {
        if($name){
            foreach($this->transliterate as $ru=>$lat){
                $name=str_replace($ru, $lat, $name);
            }
        }else{
            $letters=array();
            foreach($this->transliterate as $letter){
                $letters[]=$letter;
            }
            $letters=

            $size=sizeof($letters)-1;
            for($i=0; $i<8; $i++){
                $name.=$letters[rand(0, $size)];
            }
        }

        return strtolower($name);
    }
}